package com.link.base.base.system.utils;

import com.link.base.base.orgnization.dao.mybatis.mapper.OrgnizationMapper;
import com.link.base.base.orgnization.model.Orgnization;
import com.link.base.base.position.dao.mybatis.mapper.PositionMapper;
import com.link.base.base.position.model.Position;
import com.link.base.base.system.model.TreeNode;
import com.link.base.user.model.CoreUser;
import com.link.core.basic.service.BasicServiceException;
import com.link.core.util.BeanTools;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/***
 * 跑更新组织、职位、部门架构的线程类
 *
 * @author LiYang
 * @date 2019-12-16
 **/
public class RunOrgUpdate implements Runnable {

    private CoreUser user;

    private String type;

    public RunOrgUpdate(String type, CoreUser user) {
        this.type = type;
        this.user = user;
    }

    @Override
    public void run() {
        //基础表数据
        List<TreeNode> basicDataList = null;
        //查询参数，除了用于查询组织表外，在查询拉平表是也在使用
        Orgnization orgnization = new Orgnization();
        orgnization.setOrgType(type);
        orgnization.setCorpid(user.getCorpid());
        OrgnizationMapper orgnizationMapper = (OrgnizationMapper) BeanTools.getBean("orgnizationMapper");
        //当类型为组织时，给isOrg字段赋值为Y，查询组织
        if ("ORG".equals(type)) {
            orgnization.setIsOrg("Y");
            try {
                basicDataList = orgnizationMapper.queryAllOrgList(orgnization);
            } catch (Exception e) {
                throw new BasicServiceException("更新组织、部门、职位架构 -> 查询组织基础数据时出现异常！");
            }
        }
        //查询部门
        if ("DEPT".equals(type)) {
            orgnization.setIsOrg("");
            try {
                basicDataList = orgnizationMapper.queryAllOrgList(orgnization);
            } catch (Exception e) {
                throw new BasicServiceException("更新组织、部门、职位架构 -> 查询部门基础数据时出现异常！");
            }
        }
        //查询职位
        if ("POSTN".equals(type)) {
            Position postn = new Position();
            postn.setCorpid(user.getCorpid());
            postn.setPositionType("POSTN");
            PositionMapper positionMapper = (PositionMapper) BeanTools.getBean("positionMapper");
            try {
                basicDataList = positionMapper.queryAllPostnList(postn);
            } catch (Exception e) {
                throw new BasicServiceException("更新组织、部门、职位架构 -> 查询职位基础数据时出现异常！");
            }
        }
        //如果没有基础数据直接返回
        if (null == basicDataList && basicDataList.size() <= 0) {
            return ;
        }
        //得到拉平表组织数据
        List<TreeNode> tilingList = null;
        try {
            tilingList = orgnizationMapper.queryOrgRelList(orgnization);
        } catch (Exception e) {
            throw new BasicServiceException("更新组织、部门、职位架构 -> 查询组织结构时出现异常！");
        }
        //将拉平表组织数据转为hash
        Map<String, TreeNode> tilingMap = new HashMap<>(131072);
        tilingMap = tilingList.stream()
                .collect(Collectors.toMap(TreeNode::getCombinationId, Function.identity(), (key1, key2) -> key2));
        //得到组织、部门、职位树
        List<TreeNode> tree = TreeUtil.buildTree(basicDataList);
        //将组织树进行自下而上的遍历，找出父子关系并将所需的增删改更新至拉平的hash
        TreeUtil.backwardSequenceTraversal(tree, tilingMap);
        //遍历拉平的hash，构建需要增删改的节点
        Map<String, List<TreeNode>> result = TreeUtil.generateOperateList(tilingMap);
        TreeUtil.updateDataToDb(result, type, user);
    }
}
